<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog=true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border :data="roleList">
              <el-table-column label="序号" width="120" prop="id" />
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <!-- 利用作用域插槽获取当前行的数据 -->
                <template slot-scope="scope">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editHandle(scope.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delHandle(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height:60px">
              <!-- 分页组件 -->
              <!-- <el-pagination layout="prev,pager,next" /> -->
              <el-pagination
                :current-page.sync="queryInfo.page"
                :page-size="queryInfo.pagesize"
                layout="total, prev, pager, next"
                :total="total"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 编辑的弹窗 -->
      <el-dialog title="编辑弹层" :visible="showDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="showDialog=false">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

    </div>
  </div>
</template>
<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole } from '@/api/setting'
export default {
  data() {
    return {
      queryInfo: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      roleList: [],
      formData: {},
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    async getRoleList() {
      const { rows, total } = await getRoleList(this.queryInfo)
      this.total = total
      this.roleList = rows
    },
    async getCompanyInfo() {
      this.formData = await getCompanyInfo()
    },
    handleCurrentChange(val) {
      this.queryInfo.page = val
      this.getRoleList()
    },
    async delHandle(id) {
      // this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(async() => {
      //   await deleteRole(id)
      //   // 如果删除成功 要重新获取数据
      //   this.getRoleList()
      // }).catch(() => { console.log('取消删除') })
      // // 利用async 和 await 修改代码
      try {
        await this.$confirm('此操作将永久删除该角色, 是否继续?')
        await deleteRole(id)
        this.getRoleList()
      } catch (error) {
        this.$message('取消删除')
      }
    },
    // 关闭后要做的事情
    btnCancel() {
      this.roleForm = {
        name: '',
        description: ''
      }
      // 重置表单验证
      this.$refs.roleForm.resetFields()
    },
    async btnOK() {
      // 先做表单预校验，由于el-form的validate方法是返回一个Promise实例 所以可以使用async/await修饰
      try {
        await this.$refs.roleForm.validate()
        if (this.roleForm.id) {
          // 发起更新请求
          await updateRole(this.roleForm)
        } else {
          await addRole(this.roleForm)
        }
        // 重新获取列表数据
        await this.getRoleList()
        // 关闭弹窗
        this.showDialog = false
      } catch (error) {
        this.$message(`error-${error.message}`)
      }
    },
    async editHandle(id) {
      // 先获取当前的角色详情
      this.roleForm = await getRoleDetail(id)
      // 展示弹窗
      this.showDialog = true
    }
  }
}
</script>

<style>
</style>
